package com.xiaodou.mjf.application.basic.utils;

import com.xiaodou.mjf.application.fee.service.ServeChargeCouponService;
import com.xiaodou.mjf.application.fee.service.ServeChargePackageDetailService;
import com.xiaodou.mjf.application.fee.service.ServeChargePackageService;
import com.xiaodou.mjf.application.fee.service.ServeChargeService;
import com.xiaodou.mjf.application.fee.service.ServeService;
import com.xiaodou.mjf.entity.fee.Serve;
import com.xiaodou.mjf.entity.fee.ServeCharge;
import com.xiaodou.mjf.entity.fee.ServeChargeCoupon;
import com.xiaodou.mjf.entity.fee.ServeChargePackage;
import com.xiaodou.mjf.entity.fee.ServeChargePackageDetail;
import com.xiaodou.mjf.infrastructure.common.context.SpringContextHolder;

import java.util.List;

public class FeeUtils {

    //计算服务费金额
    private static ServeService serveService = SpringContextHolder.getBean(ServeService.class);
    private static ServeChargePackageService serveChargePackageService = SpringContextHolder.getBean(ServeChargePackageService.class);
    private static ServeChargePackageDetailService serveChargePackageDetailService = SpringContextHolder.getBean(ServeChargePackageDetailService.class);
    private static ServeChargeService serveChargeService = SpringContextHolder.getBean(ServeChargeService.class);
    private static ServeChargeCouponService serveChargeCouponService = SpringContextHolder.getBean(ServeChargeCouponService.class);

    /**
     * 计算服务费金额
     *
     * @param channelCode 渠道代码
     * @param serviceCode 服务代码
     * @param feeOrigin   原始金额
     * @return 收费金额
     */
    public static double calcFee(String channelCode, String serviceCode, double feeOrigin) {
        double feeReturn = 0.0;
        Serve serve = serveService.findByCode(serviceCode);

        String serveId = serve.getId();
        //同一时间有效的包只有一个.
        ServeChargePackage serveChargePackage = serveChargePackageService.findByServeId(serveId);
        String packageId = serveChargePackage.getId();
        //包里的收费有多个
        List<ServeChargePackageDetail> serveChargePackageDetailList = serveChargePackageDetailService.findByPackageId(packageId);

        for (ServeChargePackageDetail serveChargePackageDetail : serveChargePackageDetailList) {
            String serveChargeId = serveChargePackageDetail.getServeChargeId();

            ServeCharge serveCharge = serveChargeService.get(serveChargeId);
            //收取类型：1、一次性 2、按频率定义
            //todo:目前只有[1、一次性]
            String type = serveCharge.getType();
            //频率单位：\n1、年\n2、半年\n3、季度\n4、月\n5、周\n6、天\n7、次
            //todo:目前只有[7、次]
            Integer frequencyUnit = serveCharge.getFrequencyUnit();
            //频率值
            //todo:上面只有[7、次],所以此项目前无用
            Double frequencyValue = serveCharge.getFrequencyValue();

            double feeTmp = 0.0;
            //1、百分比2、固定值（RMB）
            Integer chargingType = serveCharge.getChargingType();
            //收取值
            Double chargingValue = serveCharge.getChargingValue();

            if (chargingType == 1) {//百分比
                feeTmp += feeOrigin * chargingValue;
            } else if (chargingType == 2) {//固定值（RMB）
                feeTmp += chargingValue;
            }

            //同一时间点有效的优惠最多有1个.
            ServeChargeCoupon serveChargeCoupon = serveChargeCouponService.findByServeChargeId(serveChargeId);

            if (null != serveChargeCoupon) {//有优惠
                //调整方式
                //1、打折调整（9折,8折）
                //2、百分点调整（负不超过手续费设置值）
                //3、固定值（RMB元最多减为0）
                Integer adjustType = serveChargeCoupon.getAdjustType();

                //具体调整值
                Double adjustRate = serveChargeCoupon.getAdjustRate();

                //TODO:CALC
                if (1 == adjustType) {//1、打折调整（9折,8折）
                    feeReturn += feeTmp * adjustRate;
                } else if (2 == adjustType) {//2、百分点调整（负不超过手续费设置值）
                    feeReturn += feeTmp * (1 - adjustRate / 100);
                } else if (3 == adjustType) {//3、固定值（RMB元最多减为0）
                    feeReturn += feeTmp - adjustRate;
                }
            }
        }
        return feeReturn;
    }
}
